﻿//using BeetleX.Buffers;
//using MessagePack;
//using System;
//using System.Buffers;
//using System.IO;
//using System.Threading;

//#nullable disable
//namespace BeetleX.XRPC.Packets
//{
//    public class MsgPacket : IParameterFormater
//    {
//        public object Decode(Options rpcOption, Type type, ArraySegment<byte> data)
//        {
//            int count = data.Count;
//            if (count == 0)
//                return type.IsValueType ? Activator.CreateInstance(type) : (object)null;
//            if (!(type == (Type)null))
//                return MessagePackSerializer.Deserialize(type, (ReadOnlyMemory<byte>)data, (MessagePackSerializerOptions)null, new CancellationToken());
//            byte[] numArray = ArrayPool<byte>.Shared.Rent(count);
//            System.Buffer.BlockCopy((Array)data.Array, data.Offset, (Array)numArray, 0, data.Count);
//            return (object)new ArraySegment<byte>(numArray, 0, count);
//        }

//        public void Encode(Options rpcOption, object data, PipeStream stream)
//        {
//            if (data == null)
//            {
//                stream.Write(0);
//            }
//            else
//            {
//                MemoryBlockCollection memoryBlockCollection = stream.Allocate(4);
//                int cacheLength = stream.CacheLength;
//                MessagePackSerializer.Serialize(data.GetType(), (Stream)stream, data, (MessagePackSerializerOptions)null, new CancellationToken());
//                memoryBlockCollection.Full(stream.CacheLength - cacheLength);
//            }
//        }
//    }
//}
